package com.soilflow.web.controller.soilflow;

import com.soilflow.common.constant.UserConstants;
import com.soilflow.common.core.controller.BaseController;
import com.soilflow.common.core.domain.AjaxResult;

import com.soilflow.common.domain.LandClassify;
import com.soilflow.common.domain.dto.LandClassifyDto;
import com.soilflow.common.domain.vo.LandClassifyVo;
import com.soilflow.common.enums.BusinessType;
import com.soilflow.common.utils.annotation.Log;
import com.soilflow.common.utils.poi.ExcelUtil;
import com.soilflow.info.service.LandClassifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 分类Controller
 *
 * @author summer
 * @date 2022-06-22
 */
@Api("分类设置")
@RestController
@RequestMapping("/land/classify")
public class LandClassController extends BaseController {
    @Autowired
    private LandClassifyService soilflowClassifyService;


    /**
     * 查询分类列表
     */
    @ApiOperation("分类设置列表")
    @GetMapping("/list")
    public AjaxResult list(LandClassifyDto dto) {
        List<LandClassifyVo> list = soilflowClassifyService.pageList(dto);
        return AjaxResult.success(list);
    }

    /**
     * 查询分类列表（排除节点）
     */
    @GetMapping("/findList")
    public AjaxResult findList() {
        List<LandClassifyVo> list = soilflowClassifyService.findList();
        return AjaxResult.success(list);
    }
    /**
     * 获取下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(LandClassifyDto dto) {
        dto.setDeptId(getDeptId());
        List<LandClassifyVo> depts = soilflowClassifyService.pageList(dto);
        return AjaxResult.success(soilflowClassifyService.buildDeptTreeSelect(depts));
    }


    /**
     * 导出分类列表
     */
//    @Log(title = "分类分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LandClassifyDto dto) {
        List<LandClassifyVo> list = soilflowClassifyService.pageList(dto);
        ExcelUtil<LandClassifyVo> util = new ExcelUtil<LandClassifyVo>(LandClassifyVo.class);
        util.exportExcel(response, list, "分类数据");
    }

    /**
     * 获取分类详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        LandClassify byId = soilflowClassifyService.getById(id);
         if (byId.getParentId()==null){
             byId.setParentId(0L);
         }
        return AjaxResult.success(byId);
    }

    /**
     * 新增分类
     */
    @Log(title = "分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LandClassify dto) {
        if (UserConstants.NOT_UNIQUE.equals(soilflowClassifyService.checkNameUnique(dto))) {
            return AjaxResult.error("新增分类'" + dto.getName() + "'失败，分类名称已存在");
        }

        return toAjax(soilflowClassifyService.add(dto));
    }

    /**
     * 修改分类
     */
    @Log(title = "分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LandClassify dto) {
        Long id = dto.getId();
        boolean b = false;
        //根据id和当前用户查询是否有权限修改
        if (!getUsername().equals("admin")) {
            b = soilflowClassifyService.getExistence(id, getUserId());
            if (!b) {
                return AjaxResult.error("没有权限修改分类");
            }
        }
        b = dto.getParentId() != null && !dto.getParentId().equals("") && dto.getParentId().equals(id);
        if (UserConstants.NOT_UNIQUE.equals(soilflowClassifyService.checkNameUnique(dto))) {
            return AjaxResult.error("修改分类'" + dto.getName() + "'失败，名称已存在");
        } else if (b) {
            return AjaxResult.error("修改分类'" + dto.getName() + "'失败，上级分类不能是自己");
        }

        return toAjax(soilflowClassifyService.updateById(dto));
    }

    /**
     * 删除分类
     */
    @Log(title = "分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {

        //根据id和当前用户查询是否有权限修改
        if (!getUsername().equals("admin")) {
            boolean b = soilflowClassifyService.getExistence(id, getUserId());
            if (!b) {
                return AjaxResult.error("没有权限修改分类");
            }
        }
        if (soilflowClassifyService.hasChildById(id)) {
            return AjaxResult.error("请先删除，下级分类");
        }
        if (soilflowClassifyService.checkExistUser(id)) {
            return AjaxResult.error("分类存在信息,不允许删除");
        }
        return toAjax(soilflowClassifyService.deleteById(id));
    }
}